#include "userinfo.h"
#include "ui_userinfo.h"

UserInfo::UserInfo(QWidget *parent,int uid) :
    QDialog(parent),
    ui(new Ui::UserInfo)
{
    m_uid = uid;
    myDatabase.getConnection();
    ui->setupUi(this);
}

UserInfo::~UserInfo()
{
    delete ui;
}

void UserInfo::on_Ensure_clicked()
{
    QSqlQuery sql;
    sql.prepare("UPDATE tb_user SET upassword = :upassword,unickname = :unickname,uname=:uname,uaddress=:uaddress,usex=:usex,utele=:utele WHERE uid = "+QString::number(m_uid));
    sql.bindValue(":upassword",ui->password->text());
    sql.bindValue(":unickname",ui->nickname->text());
    sql.bindValue(":uname",ui->name->text());
    sql.bindValue(":uaddress",ui->address->text());
    sql.bindValue(":usex",ui->sex->text());
    sql.bindValue(":utele",ui->tele->text());
    sql.exec();
    QMessageBox::information(this,"提示","信息修改成功！",QMessageBox::Yes);
}


void UserInfo::on_display_clicked()
{
    QSqlQuery sql;
    sql.prepare("SELECT uaccount,upassword,unickname,uname,usex,utele,uaddress,upoints FROM tb_user WHERE uid = "+QString::number(m_uid));
    sql.exec();
    sql.first();
    ui->account->setText(sql.value("uaccount").toString());
    ui->password->setText(sql.value("upassword").toString());
    ui->name->setText(sql.value("uname").toString());
    ui->nickname->setText(sql.value("unickname").toString());
    ui->sex->setText(sql.value("usex").toString());
    ui->tele->setText(sql.value("utele").toString());
    ui->address->setText(sql.value("uaddress").toString());
    ui->points->setText(sql.value("upoints").toString());
}


void UserInfo::on_VIP_clicked()
{
    QSqlQuery sql;
    qDebug()<<"info "<<m_uid;
    sql.prepare("SELECT upoints,uaccount,upassword FROM tb_user WHERE uid = " + QString::number(m_uid));
    sql.exec();
    sql.first();
    float points = sql.value("upoints").toFloat();
    QString account = sql.value("uaccount").toString();
    QString password = sql.value("upassword").toString();
    if(points > 20){
        QSqlQuery sql;
        sql.prepare("SELECT uaccount FROM tb_user_VIP WHERE uaccount = :account AND upassword = :password");
        sql.bindValue(":account",account);
        sql.bindValue(":password",password);
        sql.exec();
        if(sql.first()){
            QMessageBox::information(this,"提示","您已经是VIP用户！",QMessageBox::Yes);
        }
        else{
            sql.prepare("INSERT INTO tb_user_VIP(uaccount,upassword) VALUES(:account,:password)");
            sql.bindValue(":account",account);
            sql.bindValue(":password",password);
            sql.exec();
            QMessageBox::information(this,"提示","升级VIP成功！您当前的积分是："+QString::number(points),QMessageBox::Yes);
        }
    }
    else{
        QMessageBox::information(this,"提示","积分不足，升级失败！(升级VIP需要20积分)",QMessageBox::Yes);
    }
}

